
define(["jquery", "easy-admin","xm-select"], function ($, ea) {


    var gid =$("#gid").val();
    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'mall.mendian/index?id='+gid,
        add_url: 'mall.mendian/add?id='+gid,
        edit_url: 'mall.mendian/edit',
        delete_url: 'mall.mendian/delete',
    };

    var Controller = {
        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh',
                    [{
                        text: '添加',
                        url: init.add_url,
                        method: 'open',
                        auth: 'add',
                        class: 'layui-btn layui-btn-normal layui-btn-sm',
                        icon: 'fa fa-plus ',
                        extend: 'data-full="true"',
                    }],
                   ],
                cols: [[
                    {type: "checkbox"},
                    {field: 'id', width: 80, title: 'ID'},
                    {field: 'goods_name', minWidth: 80, title: '商品名称'},
                    {field: 'store_title', minWidth: 100, title: '门店名称'},
                    {field: 'store_mobile', minWidth: 100, title: '门店电话'},
                    {field: 'store_lng', minWidth: 80, title: '经度', search: false},
                    {field: 'store_lat', width: 100, title: '纬度',search: false},
                    {field: 'store_name', width: 100, title: '门店地址'},
                    {field: 'create_time', minWidth: 80, title: '创建时间', search: 'range'},
                    {
                        width: 250,
                        title: '操作',
                        templet: ea.table.tool,
                        operat: [
                            [{
                                text: '编辑',
                                url: init.edit_url,
                                method: 'open',
                                auth: 'edit',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                                extend: 'data-full="true"',
                            }],
                            'delete']
                    }
                ]],
            });

            ea.listen();
        },
        add: function () {
            var demo1 = xmSelect.render({
                el: '#demo1',
                on: function (data) {
                    var arr = data.arr;
                    if (arr.length > 0) {
                        for (let i = 0; i < arr.length; i++) {
                            $("#store_str").val(arr[i].value) ;
                        }
                    }
                },
                autoRow: true,
                radio: true,
                toolbar: { show: true },
                filterable: true,
                remoteSearch: true,
                remoteMethod: function(val, cb, show){
                    //这里如果val为空, 则不触发搜索
                    if(!val){
                        return cb([]);
                    }
                    $.ajax({
                        type: "GET",
                        url: "/api/index/getRegionSearchResult",
                        data: {keyword: val},
                        dataType: "json",
                        success: function (data) {
                            console.log(data);
                            cb(data.data.location);
                        }
                    })
                }
            })
            ea.listen();
        },
        edit: function () {
            var demo1 = xmSelect.render({
                el: '#demo1',
                on: function (data) {
                    var arr = data.arr;
                    if (arr.length > 0) {
                        for (let i = 0; i < arr.length; i++) {
                            $("#store_str").val(arr[i].value) ;
                        }
                    }
                },
                autoRow: true,
                radio: true,
                toolbar: { show: true },
                filterable: true,
                remoteSearch: true,
                remoteMethod: function(val, cb, show){
                    //这里如果val为空, 则不触发搜索
                    if(!val){
                        return cb([]);
                    }
                    $.ajax({
                        type: "GET",
                        url: "/api/index/getRegionSearchResult",
                        data: {keyword: val},
                        dataType: "json",
                        success: function (data) {
                            console.log(data);
                            cb(data.data.location);
                        }
                    })
                }
            })
            ea.listen();
        }
    };
    return Controller;
});